package com.gm.wholesale.module.voice;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;

import com.gm.commonlib.util.logUtils.LogUtil;
import com.gm.commonlib.views.popupwindow.Popupwindow;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.util.ResourceUtil;
import com.youth.banner.util.LogUtils;

public class VoiceManger {
    public static final String TYPE_LOCAL = "local";
    public static VoiceManger voiceManger;

    public static VoiceManger getVoiceManger() {
        if (voiceManger == null) {
            voiceManger = new VoiceManger();
        }
        return voiceManger;
    }

    private SpeechRecognizer mIat;

    private MyRecognizerListener myRecognizerListener;

    public void init(Context context) {
        if (mIat == null) {

            mIat = SpeechRecognizer.createRecognizer(context, new InitListener() {
                @Override
                public void onInit(int i) {
                    LogUtils.d("科大sdk 初始化: " + i);
                }
            });
            if (mIat==null)
                return;
            //设置语法ID和 SUBJECT 为空，以免因之前有语法调用而设置了此参数；或直接清空所有参数，具体可参考 DEMO 的示例。
            mIat.setParameter(SpeechConstant.CLOUD_GRAMMAR, null);
            mIat.setParameter(SpeechConstant.SUBJECT, null);

            //设置返回结果格式，目前支持json,xml以及plain 三种格式，其中plain为纯听写文本内容
            mIat.setParameter(SpeechConstant.RESULT_TYPE, "plain");
            //“cloud” 在线 local离线
            //  mIat.setParameter( SpeechConstant.ENGINE_TYPE, engineType );
            mIat.setParameter(SpeechConstant.ENGINE_TYPE, "cloud");
         //  if (mEngineType.equals(SpeechConstant.TYPE_LOCAL)) {
                // 设置本地识别资源
              //  mIat.setParameter(ResourceUtil.ASR_RES_PATH, getResourcePath(context));
           // }
            //设置语音输入语言，zh_cn为简体中文
            mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
            //设置结果返回语言
            mIat.setParameter(SpeechConstant.ACCENT, "mandarin");
            // 设置语音前端点:静音超时时间，单位ms，即用户多长时间不说话则当做超时处理
            //取值范围{1000～10000}
            mIat.setParameter(SpeechConstant.VAD_BOS, "9000");
            //设置语音后端点:后端点静音检测时间，单位ms，即用户停止说话多长时间内即认为不再输入，
            //自动停止录音，范围{0~10000}
            mIat.setParameter(SpeechConstant.VAD_EOS, "100");
            mIat.setParameter("dwa", "wpgs");
            //设置标点符号,设置为"0"返回结果无标点,设置为"1"返回结果有标点
            mIat.setParameter(SpeechConstant.ASR_PTT, "0");
        }
    }

        private String getResourcePath(Context context) {
            StringBuffer tempBuffer = new StringBuffer();
            //识别通用资源
            tempBuffer.append(ResourceUtil.generateResourcePath(context, ResourceUtil.RESOURCE_TYPE.assets, "iat/common.jet"));
            tempBuffer.append(";");
            tempBuffer.append(ResourceUtil.generateResourcePath(context, ResourceUtil.RESOURCE_TYPE.assets, "iat/sms_16k.jet"));
            //识别8k资源-使用8k的时候请解开注释
            return tempBuffer.toString();
        }

    // todo 开始语音输入检测
    public synchronized void startVoice(Context context, IVoiceListener listener) {
        //初始化识别无UI识别对象
        //使用SpeechRecognizer对象，可根据回调消息自定义界面；
        if (mIat == null) {
            init(context);
        }
        if (myRecognizerListener == null) {
            myRecognizerListener = new MyRecognizerListener(listener);
        }
        if(mIat!=null){
            //开始识别，并设置监听器
            mIat.startListening(myRecognizerListener);
        }

    }

    class MyRecognizerListener implements RecognizerListener {
        IVoiceListener listener;

        public MyRecognizerListener(IVoiceListener listener) {
            this.listener = listener;
        }

        @Override
        public void onVolumeChanged(int i, byte[] bytes) {
            listener.onVolumeChanged();
        }

        @Override
        public void onBeginOfSpeech() {
            if (listener!=null){
                listener.onBeginOfSpeech();
            }

            LogUtil.d("语音返回数据 onBeginOfSpeech： ");
        }

        @Override
        public void onEndOfSpeech() {
            listener.onEndOfSpeech();
        }
       String result="";
        @Override
        public void onResult(RecognizerResult recognizerResult, boolean b) {
            LogUtil.d("科大 语音返回数据 获取数据： " + recognizerResult.getResultString());
             if (TextUtils.isEmpty(recognizerResult.getResultString())){
                 stop();
                 Popupwindow.hiddenPop();
                 listener.onResult(result);
             }
             else {
                 result=recognizerResult.getResultString();
                 listener.onTimelyData(result);
             }


        }

        @Override
        public void onError(SpeechError speechError) {
            listener.onError(speechError.getErrorDescription());

        }

        @Override
        public void onEvent(int i, int i1, int i2, Bundle bundle) {
        }
    }

    public void stop() {
        if (mIat != null) {
            mIat.stopListening();
        }
    }

    public void destory() {
        if (mIat != null) {
            stop();
            mIat = null;
            myRecognizerListener=null;
            voiceManger=null;
        }
    }

}
